package com.zbkj.service.listener;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.zbkj.common.enums.EventMessageType;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.bsy.EventMsg;
import com.zbkj.service.service.BsyUserService;
import com.zbkj.service.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queuesToDeclare = {@Queue("shop.score.queue")})
@Slf4j
public class ShopScoreMQListener {
    @Autowired
    private UserService userService;


    @RabbitHandler
    public void shopHandler(JSONObject eventMessage, Message message, Channel channel){
        log.info("监听到消息ProductOrderMQListener message消息内容:{}", message);
        try {
            EventMsg msg = JSONObject.toJavaObject(eventMessage,EventMsg.class);
            if(msg.getBizType().equalsIgnoreCase(EventMessageType.SHOP_DOCTOR_SCORE_ADD.name())){
                userService.setScoreByBsyDoctor(msg);
            }else if(msg.getBizType().equalsIgnoreCase(EventMessageType.SHOP_CUSTOM_SCORE_ADD.name())){
                userService.setScoreByBsyCustom(msg);
            }

        } catch (Exception e) {
            //处理业务异常，还有进行其他操作，比如记录失败原因
            log.error("消费失败:{}", eventMessage);
            throw new CrmebException("监听业务系统消息-消费失败");
        }
        log.info("消费成功:{}", eventMessage);

    }
}
